<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" >
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/plugins/layui/css/layui.css" media="all">
    <link rel="stylesheet" type="text/css" href="/build/css/css/font-awesome.min.css">
    <link rel="stylesheet" type="text/css" href="/css/my-style.css">
    <link th:href="@{/ajax/libs/jquery-ztree/3.5/css/metro/zTreeStyle.css}" rel="stylesheet"/>
</head>
<style>
    .layui-form-label {
        width: 86px;
        padding: 9px 9px;
    }
</style>
<body>
<!-- 弹框content -->
<div id="modal-content1" style="padding:20px;">
    <form class="layui-form" action="">
        <input type="hidden" id="projectId" th:value="${project.getId()}">
        <div class="layui-form-item">
            <label class="layui-form-label">项目名 <span style="color: #ff393c;">★</span>：</label>
            <div class="layui-input-block">
                <input type="text" name="projectName" required lay-verify="required"
                       placeholder="请输入项目名"
                       autocomplete="off" class="layui-input"
                       th:value="${project.getProjectName()}">
            </div>
        </div>
        <div class="layui-form-item">
            <input name="deptId" type="hidden" id="treeId"/>
            <label class="layui-form-label">选择部门 <span style="color: #ff393c;">★</span>：</label>
                <!--<select  name="deptName" onclick="selectDeptTree()"-->
                       <!--lay-verify="required" xm-select="dept"-->
                       <!--readonly="true" id="treeName">-->
                <div class="layui-input-block" style="height: 200px;overflow-y: scroll">
                    <div id="tree" class="ztree"></div>
                </div>
                <!--<input class="layui-input" type="text" name="deptName" onclick="selectDeptTree()"-->
                       <!--lay-verify="required" xm-select="dept"-->
                       <!--readonly="true" id="treeName" >-->
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">开始时间 <span style="color: #ff393c;">★</span>：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="startDate" name="startDate"
                       placeholder="yyyy-MM-dd"
                       th:value="${#dates.format(project.getStartDate(), 'yyyy-MM-dd')}">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">结束时间 <span style="color: #ff393c;">★</span>：</label>
            <div class="layui-input-inline">
                <input type="text" class="layui-input" id="endDate" th:name="endDate"
                       placeholder="yyyy-MM-dd"
                       th:value="${#dates.format(project.getEndDate(), 'yyyy-MM-dd')}">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">启用/禁用</label>
            <div class="layui-input-block">
                <input type="checkbox" name="status" id="status" lay-skin="switch" lay-text="启用|禁用"
                       lay-filter="switchStatus" th:checked="${project.status eq '0'} ">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" placeholder="请输入内容" id="remark"
                          class="layui-textarea" th:text="${project.getRemark()}"></textarea>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 70%;margin-top: 8%">
                <button class="layui-btn" lay-submit="" lay-filter="formDemo">立即提交</button>
                <button type="reset" class="layui-btn layui-btn-primary">重置</button>
            </div>
        </div>
    </form>
</div>
<script src="/plugins/layui/layui.js"></script>
<script type="text/javascript" src="../../static/js/jquery.min.js"
        th:src="@{/js/jquery.min.js}"></script>
<script th:src="@{/ajax/libs/jquery-ztree/3.5/js/jquery.ztree.all-3.5.js}"></script>
<script src="/js/ry-ui.js"></script>
<script>
    /*用户管理-新增-选择部门树*/
    function selectDeptTree() {
        var treeId = $("#treeId").val();
        var deptId = treeId == null || treeId == "" ? "100" : treeId;
        var url = "/system/dept/selectDeptTree/" + deptId;
        layer.open({
            title: "选择部门",
            type: 2,
            content: url,
            area: ['60%', '85%'],
            shadeClose: true
        });
    }
</script>
<script>
    layui.config({
        base: '/plugins/layui/'    //此处写的相对路径, 实际以项目中的路径为准
    }).extend({
        formSelects: 'formSelects'
    });
    // 增加项目表单
    layui.use(['form', 'layedit', 'laydate', 'jquery', 'formSelects'], function () {
        var form = layui.form
            , layer = layui.layer
            , layedit = layui.layedit
            , laydate = layui.laydate,
            formSelects = layui.formSelects;

        //常规用法
        laydate.render({
            elem: '#startDate'
        });
        laydate.render({
            elem: '#endDate'
        });

        //创建一个编辑器
        var editIndex = layedit.build('LAY_demo_editor');

        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [/(.+){6,12}$/, '密码必须6到12位']
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });

        //监听指定开关
        form.on('switch(switchStatus)', function (data) {
            this.value = this.checked ? "0" : "1";
        });

        var $ = layui.$;
        //监听提交
        form.on('submit(formDemo)', function (data) {
            data.field.status = data.field.status ? "0" : "1";
            var deptId = selectCheckeds();
            if(deptId==''||deptId==null||deptId==undefined){
                layer.msg('请选择部门！')
                return false;
            }
            data.field.deptId = deptId;
            $.ajax({
                type: 'PUT',
                url: "/project/update/" + $("#projectId").val(),
                data: data.field,
                success: function (res) {
                    if (res.code == 0) {
                        layer.msg("修改成功", {
                            time: 800
                        }, function () {
                            // 关闭自己
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                            window.parent.location.reload();
                        });
                    } else {
                        layer.msg("修改失败" + res.msg);
                    }
                }
            });
            return false;
        });

    });
    function selectCheckeds() {
        var menuIds = "";
        var treeNodes = tree.getCheckedNodes(true);
        for (var i = 0; i < treeNodes.length; i++) {
            if (treeNodes[i].getCheckStatus().half != true) {
                if (menuIds == "") {
                    menuIds = treeNodes[i].id;
                } else {
                    menuIds += ("," + treeNodes[i].id);
                }
            }
        }
        return menuIds;
    }
    function selectCheckNode(treeName, node) {
        if (treeName == node.name) {
            tree.selectNode(node, true);
        }
    }
</script>
<script>
    // 树结构初始化加载
    var setting = {
        check: {enable: true, nocheckInherit: true, chkboxType: {"Y": "ps", "N": "ps"}},
        view: {selectedMulti: false}, data: {key: {title: "title"}, simpleData: {enable: true}},
        callback: {
            onClick: function (event, treeId, treeNode) {
                var treeId = treeNode.id;
                var treeName = treeNode.name;
                $("#treeId").val(treeId);
                $("#treeName").val(treeName);
            }
        }
    }, tree, loadTree = function () {
        $.get("/system/dept/treeData?projectId="+$("#projectId").val(), function (data) {
            var treeName = $("#treeName").val();
            tree = $.fn.zTree.init($("#tree"), setting, data);
            // 展开第一级节点
            var nodes = tree.getNodesByParam("level", 0);
            for (var i = 0; i < nodes.length; i++) {
                tree.expandNode(nodes[i], true, false, false);
                selectCheckNode(treeName, nodes[i]);
            }
            // 展开第二级节点
            nodes = tree.getNodesByParam("level", 1);
            for (var i = 0; i < nodes.length; i++) {
                tree.expandNode(nodes[i], true, false, false);
                selectCheckNode(treeName, nodes[i]);
            }
            // 展开第二级节点
            nodes = tree.getNodesByParam("level", 2);
            for (var i = 0; i < nodes.length; i++) {
                tree.expandNode(nodes[i], true, false, false);
                selectCheckNode(treeName, nodes[i]);
            }
        }, null, null, "正在加载，请稍后...");
    };

    $(function () {
        loadTree();
    });
</script>
</body>
</html>